package com.platform.wechat.service.impl;

import com.platform.wechat.common.Constant;
import com.platform.wechat.common.JsonBean;
import com.platform.wechat.entity.Hospital;
import com.platform.wechat.mapper.HospitalMapper;
import com.platform.wechat.service.HospitalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author YP
 * @company ~~
 * @create 2021-07-28 11:51
 */
@Service
public class HospitalServiceImpl implements HospitalService {



    @Autowired
    private HospitalMapper hospitalMapper;

    /**
     * 根据id查询医院信息
     * @param id
     * @return
     */
    @Override
    public JsonBean selectHospitalMsg(Integer id) {
        return new JsonBean(0, "ok", hospitalMapper.selectByPrimaryKey(id));
    }

    /**
     * 查询所有医院信息
     * @return
     */
    @Override
    public JsonBean selectAllHospital(String hospitalAddress) {
        //System.out.println("进入service层！");
        if (hospitalAddress!=null){
            if (hospitalAddress.equals("全部")){
                hospitalAddress = null;
            }
        }
        return new JsonBean(0, "ok", hospitalMapper.selectAllHospital(hospitalAddress));
    }

    @Override
    public JsonBean vagueQuery(String hospitalName) {
        //System.out.println("进入service");
        return new JsonBean(0, "ok", hospitalMapper.vagueQuery(hospitalName));
    }

    /** 
    * @Description: 根据科室查询医院
    * @Param: [officesName] 
    * @return: com.platform.wechat.common.JsonBean 
    * @Author: guopeixin
    * @Date: 2021/7/31 10:30
    */
    @Override
    public JsonBean selectHospitalByofficesName(String officesName,String hospitalAddress, String hospitalName) {
        if (hospitalAddress!=null){
            if (hospitalAddress.equals("全部")){
                hospitalAddress = null;
            }
        }
        return new JsonBean(0, "ok", hospitalMapper.selectHospitalByofficesName(officesName,hospitalAddress, hospitalName));
    }

    /** 
    * @Description: 根据医院ID,科室ID查询医院信息及科室
    * @Param: [hospitalId, chiefofficesId]
    * @return: com.platform.wechat.common.JsonBean 
    * @Author: guopeixin
    * @Date: 2021/7/31 16:16
    */
    @Override
    public JsonBean selectOfficesNameByHospitalId(Integer hospitalId, Integer chiefofficesId, Integer officesId, String hospitalAddress, String officesName, String hospitalName) {
        return new JsonBean(0, "ok", hospitalMapper.selectOfficesNameByHospitalId(hospitalId, chiefofficesId, officesId, hospitalAddress, officesName, hospitalName));
    }

    /** 
    * @Description: 根据医院ID,科室ID查询一条信息
     * @Param: [hospitalId, officesName]
    * @return: com.platform.wechat.common.JsonBean 
    * @Author: guopeixin
    * @Date: 2021/8/1 16:26
    */
    @Override
    public JsonBean selectOne(Integer hospitalId, String officesName, Integer officesId, String hospitalAddress, String hospitalName) {
        return new JsonBean(0, "ok", hospitalMapper.selectOne(hospitalId, officesName, officesId, hospitalAddress, hospitalName));
    }

    @Override
    public JsonBean selectOneByONO(Integer hospitalId, Integer officesId) {
        return new JsonBean(0, "ok", hospitalMapper.selectOneByONO(hospitalId, officesId));
    }

    @Override
    public JsonBean insertHospitalDistance(Double longitude, Double latitude) {
        JsonBean jsonBean = new JsonBean(-1, "获取地理位置信息失败", null);
        List<Hospital> hospitals = hospitalMapper.selectAllHospitalToDistance();
        if(hospitals.size()!=0){
            for (Hospital hospital : hospitals) {
                //System.err.println(hospital);
                double distance = Constant.getDistance(longitude, latitude, hospital.getLongitude(), hospital.getLatitude());
                Hospital hos = new Hospital();
                hos.setHospitalId(hospital.getHospitalId());
                System.err.println(distance+"/////////////////////////////////");
                hos.setDistance(distance);
                int i = hospitalMapper.updateByPrimaryKeySelective(hos);
                if (i<0){
                    jsonBean.setCode(-1);
                    jsonBean.setMsg("获取"+hospital.getHospitalName()+"的地理位置信息失败");
                    break;
                }else {
                    jsonBean.setCode(0);
                    jsonBean.setMsg("ok");
                }
            }
        }
        return jsonBean;
    }

    @Override
    public JsonBean selectAllSortByDistance() {
        return new JsonBean(0, "ok", hospitalMapper.selectAllSortByDistance());
    }

}
